package com.cgtong.android.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.cgtong.android.App;
import com.cgtong.android.R;
import com.cgtong.android.activity.MyReportsActivity;
import com.cgtong.android.activity.ProblemDetailActivity;
import com.cgtong.android.bean.ProblemBean;
import com.cgtong.android.util.CommonUtils;
import com.cgtong.android.widget.GlideApp;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.cgtong.android.fragment.MyReportsFragment.CLOSE;
import static com.cgtong.android.fragment.MyReportsFragment.EVALUATE;
import static com.cgtong.android.fragment.MyReportsFragment.TOVOID;
import static com.cgtong.android.fragment.MyReportsFragment.UNREPORTED;

/**
 * Created by chunc on 2017/10/18.
 */

public class ReportsAdapter extends BaseQuickAdapter<ProblemBean, BaseViewHolder> {

    public ReportsAdapter(@Nullable List<ProblemBean> data) {
        super(R.layout.adapter_my_reports, data);
    }

    @Override
    protected void convert(final BaseViewHolder helper, ProblemBean item) {

        ImageView avatarImg = helper.getView(R.id.avatar_img);
        ImageView deleteImg = helper.getView(R.id.delete_img);
        if (item.getAvasterId() >= 1 && item.getAvasterId() <= 12) {
            CommonUtils.setUserAvatar(mContext, avatarImg, item.getAvasterId());
        } else {
            LogUtils.d("avatarId错误");
            avatarImg.setImageResource(R.drawable.head_1);
        }

        helper.setText(R.id.name_txt, item.getNickName());
        helper.setText(R.id.date_txt, item.getCreateTime());
        helper.setText(R.id.location_txt, item.getAddress());
        if (item.getStatus() == CLOSE
                || item.getStatus() == EVALUATE
                || item.getStatus() == TOVOID) {
            deleteImg.setVisibility(View.VISIBLE);
        } else {
            deleteImg.setVisibility(View.GONE);
        }
        deleteImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getOnItemChildClickListener() != null) {
                    getOnItemChildClickListener().onItemChildClick(ReportsAdapter.this, v, helper.getLayoutPosition() - getHeaderLayoutCount());
                }
            }
        });
        //展示图片
        List<ProblemBean.ResourceBean> picList = item.getResourceList();
        if (picList != null && picList.size() > 0) {
            helper.setGone(R.id.grid_view, true);
            GridView gridView = helper.getView(R.id.grid_view);
            gridView.setAdapter(new ProblemPictureAdapter(picList, item));
        } else {
            helper.setGone(R.id.grid_view, false);
        }


        //处理进度
        List<ProblemBean.StatusChangeBean> stepList = item.getStatusChangeList();
        if (stepList != null && stepList.size() > 0) {
            helper.setGone(R.id.step_fl, true);
            RecyclerView stepRecyclerView = helper.getView(R.id.step_recycler_view);
//            stepRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
            stepRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
            stepRecyclerView.setAdapter(new HorizontalStepAdapter(mContext, item.getStatus(), item.getIsExpired(), item.getStatusChangeList()));
        } else {
            helper.setGone(R.id.step_fl, false);
        }

        //录音
        String voiceUrl = item.getVoiceUrl();
        if (!TextUtils.isEmpty(voiceUrl) && voiceUrl.startsWith("http")) {
            helper.setGone(R.id.audio_ll, true);
            helper.setText(R.id.play_audio_duration_txt, item.getVoiceTime() + "\"");
        } else {
            helper.setGone(R.id.audio_ll, false);
        }
        //文字
        TextView descTxt = helper.getView(R.id.content_txt);
        if (!TextUtils.isEmpty(item.getDesc())) {
            descTxt.setVisibility(View.VISIBLE);
            descTxt.setText(item.getDesc());
            if (TextUtils.isEmpty(voiceUrl)) {
                descTxt.setMaxLines(5);
            } else {
                descTxt.setMaxLines(3);
            }
//            helper.setGone(R.id.content_txt, true);
//            helper.setText(R.id.content_txt, item.getDesc());

        } else {
            descTxt.setVisibility(View.GONE);
//            helper.setGone(R.id.content_txt, false);
        }
    }


    class ProblemPictureAdapter extends BaseAdapter {

        private List<ProblemBean.ResourceBean> mList;
        private ProblemBean mProblemBean;

        public ProblemPictureAdapter(List<ProblemBean.ResourceBean> mList, ProblemBean problemBean) {
            this.mList = mList;
            this.mProblemBean = problemBean;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public ProblemBean.ResourceBean getItem(int position) {
            if (position <= mList.size() - 1) {
                return mList.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mLayoutInflater.inflate(R.layout.layout_picture, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if (getItem(position) != null) {
                GlideApp.with(mContext).load(getItem(position).getSmallContent())
                        .placeholder(R.drawable.icon_default_loading)
                        .error(R.drawable.icon_default_error)
                        .dontAnimate().into(holder.imageView);
                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        ImagePreviewActivity.start(mContext, mList, position);
                        boolean canDelete = false;
                        boolean canEvaluate = false;
                        if (mProblemBean.getStatus() == CLOSE
                                || mProblemBean.getStatus() == EVALUATE
                                || mProblemBean.getStatus() == TOVOID
                                || mProblemBean.getStatus() == UNREPORTED) {
                            canDelete = true;
                        }
                        if (mProblemBean.getStatus() == CLOSE || mProblemBean.getStatus() == EVALUATE) {
                            canEvaluate = true;
                        }
//                }
                        ProblemDetailActivity.startForResult((MyReportsActivity) mContext, mProblemBean.getId(), canDelete, canEvaluate, position);

                    }
                });
            } else {
                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean canDelete = false;
                        boolean canEvaluate = false;
                        if ((mProblemBean.getStatus() == CLOSE
                                || mProblemBean.getStatus() == EVALUATE
                                || mProblemBean.getStatus() == TOVOID
                                || mProblemBean.getStatus() == UNREPORTED)
                                && mProblemBean.getCreateUserId() == App.getInstance().getCurrentAccount().getUserId()) {
                            canDelete = true;
                        }
                        if ((mProblemBean.getStatus() == CLOSE || mProblemBean.getStatus() == EVALUATE)
                                && mProblemBean.getCreateUserId() == App.getInstance().getCurrentAccount().getUserId()) {
                            canEvaluate = true;
                        }
                        ProblemDetailActivity.start(mContext, mProblemBean.getId(), canDelete, canEvaluate);
                    }
                });
            }
            return convertView;
        }


        class ViewHolder {

            @BindView(R.id.img_view)
            ImageView imageView;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }

    }

}
